package cn.thens.kdroid.core.util

import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject

class RxEvent<T> private constructor(private val subject: Subject<T>) {
    fun post(data: T) {
        subject.onNext(data)
    }

    fun asObservable(): Observable<T> {
        return subject.hide()
    }

    companion object {
        fun <T> create(subject: Subject<T> = PublishSubject.create<T>()): RxEvent<T> {
            return RxEvent(subject)
        }

        fun <T> create(defaultValue: T): RxEvent<T> {
            return create(BehaviorSubject.createDefault(defaultValue))
        }
    }
}